import type { FormRules } from 'naive-ui';

/**
 * 表单验证规则
 */
export const formRules: FormRules = {
  required: {
    required: true,
    message: '此字段为必填项',
    trigger: ['blur', 'input']
  },
  ruleName: {
    required: true,
    message: '请输入规则名称',
    trigger: ['blur', 'input']
  },
  ruleCode: {
    required: true,
    message: '请输入规则编码',
    trigger: ['blur', 'input']
  },
  fileType: {
    required: true,
    message: '请选择文件类型',
    trigger: 'change'
  },
  sourceType: {
    required: true,
    message: '请选择数据源类型',
    trigger: 'change'
  },
  targetDatabase: {
    required: true,
    message: '请输入目标数据库',
    trigger: ['blur', 'input']
  },
  targetTable: {
    required: true,
    message: '请输入目标表名',
    trigger: ['blur', 'input']
  },
  loadStrategy: {
    required: true,
    message: '请选择加载策略',
    trigger: 'change'
  },
  status: {
    required: true,
    message: '请选择状态',
    trigger: 'change'
  }
};

/**
 * 获取操作确认文本
 * @param operateType 操作类型
 * @returns 确认文本
 */
export function getConfirmText(operateType: AntDesign.TableOperateType): string {
  const textMap: Record<AntDesign.TableOperateType, string> = {
    add: '确认新增',
    edit: '确认修改',
    view: '确认'
  };
  return textMap[operateType] || '确认';
}

/**
 * 获取操作标题
 * @param operateType 操作类型
 * @returns 标题文本
 */
export function getTitle(operateType: AntDesign.TableOperateType): string {
  const titleMap: Record<AntDesign.TableOperateType, string> = {
    add: '新增规则',
    edit: '编辑规则',
    view: '查看规则'
  };
  return titleMap[operateType] || '操作';
}

/**
 * 创建必填验证规则
 * @param message 错误消息
 * @returns 验证规则
 */
export function createRequiredRule(message: string) {
  return {
    required: true,
    message,
    trigger: ['blur', 'input']
  };
}

/**
 * 创建长度验证规则
 * @param min 最小长度
 * @param max 最大长度
 * @param message 错误消息
 * @returns 验证规则
 */
export function createLengthRule(min: number, max: number, message?: string) {
  return {
    min,
    max,
    message: message || `长度应在 ${min} 到 ${max} 个字符之间`,
    trigger: ['blur', 'input']
  };
}

/**
 * 创建正则验证规则
 * @param pattern 正则表达式
 * @param message 错误消息
 * @returns 验证规则
 */
export function createPatternRule(pattern: RegExp, message: string) {
  return {
    pattern,
    message,
    trigger: ['blur', 'input']
  };
}